<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js"></script>

        <script type="text/javascript">
            
            // I got the idea from an IBM tutorial.
            function _request(method, xhrArgs) {
                if (!xhrArgs.url) {
                    var deferred = new dojo.Deferred();
                    deferred.errback();
                    return deferred;
                }
                xhrArgs.url = xhrArgs.url;
                return dojo[method](xhrArgs);
            }

function createMarca() {
    var m = {"nome": dojo.byId("cnome").value };
    var marca = dojo.toJson(m);

    //Save.
    var deferred = this._request("rawXhrPost", {
        url: "/Xulapa-war/rest/xulapa/marcas",
        handleAs: "json",
        postData: marca,
        headers: { "Content-Type": "application/json"}
    });
    deferred.addCallback(this, function(value) {
        alert("Saved! " + value);
    });
    deferred.addErrback(this, function(value) {
          alert("Error: "  + value);
    });
}

function readMarcas() {
    var deferred = _request("xhrGet", {
        url: "/Xulapa-war/rest/xulapa/marcas",
        handleAs: "json",
        headers: { "Accept": "application/json"}
    });
    deferred.addCallback(this, function(data) {
        var marcasDiv = dojo.byId("marcas");
        dojo.forEach(data, function(marca) {
            dojo.place(dojo.create("p", {innerHTML: "Marca[id=" + marca.id + ", nome=" + marca.nome + "]"}), marcasDiv);
        });
    });
    deferred.addErrback(this, function(value) {
        alert("Error while retrieving books: "  + value);
    });

}

function updateMarca() {
    var m = {"id": dojo.byId("uid").value, "nome": dojo.byId("unome").value };
    var marca = dojo.toJson(m);

    //Save.
    var deferred = this._request("rawXhrPut", {
        url: "/Xulapa-war/rest/xulapa/marcas",
        handleAs: "json",
        putData: marca,
        headers: { "Content-Type": "application/json"}
    });
    deferred.addCallback(this, function(value) {
        alert("Updated! " + value);
    });
    deferred.addErrback(this, function(value) {
          alert("Error: "  + value);
    });
}

function deleteMarca() {
    var id = dojo.byId("did").value;
    var deferred = _request("xhrDelete", {
        url: "/Xulapa-war/rest/xulapa/marcas/" + id,
        handleAs: "json"
    });
    deferred.addCallback(this, function(value) {
        alert("Marca deleted.");
    });
    deferred.addErrback(this, function(value) {
          alert("Error while deleting marca: "  + value);
    });
}

        </script>


    </head>
    <body>
        <input type="button" id="r" value="retrieve" onclick="readMarcas()"/>

        <br />
        <div id="marcas"></div>

        <hr>

        Nova marca: <input type="text" id="cnome" size="15"> <input type="button" id="bcreate" value="create" onclick="createMarca()"/>

        <hr>

        Atualizar marca:<br/>
        Id: <input type="text" id="uid" size="3"> <br/>
        Nome: <input type="text" id="unome" size="15"><br/>
        <input type="button" id="bupdate" value="update" onclick="updateMarca()"/>

        <hr>

        Apagar marca: <input type="text" id="did" size="3"> <input type="button" id="bdelete" value="delete" onclick="deleteMarca()"/>
    </body>
</html>
